﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using jSLApp.Client.MainService;
using jSLCore.Client.Presentation.BaseControl;
using jSLCore.Client.Presentation.BaseControl.DialogMessage;
using jSLCore.Client.Presentation.jBinding.BaseValidationRules;
using jSLCore.Utility.Extensions;

namespace jSLApp.Client.Forms
{
    public partial class CommentList : DialogControl
    {
        private readonly int _filmId;

        public CommentList(int filmId):this()
        {
            _filmId = filmId;
        }

        public CommentList()
        {
            InitializeComponent();
        }

        #region dialog

        public override bool IsStandartOkCancelEnabled
        {
            get { return true; }
        }

        public override bool IsOkVisible
        {
            get { return false; }
        }

        public override void LoadInitialData()
        {
          
            DoWork("Загрузка комментариев",(sender, args) =>
                                               {
                                                   args.Result = Communication.Proxy.GetComments(args.Int);
                                               },(o, eventArgs) =>
                                                                     {
                                                                         if(eventArgs.Error==null)
                                                                         {
                                                                             Comments =
                                                                                 ((IEnumerable<Comment>)
                                                                                  eventArgs.Result).
                                                                                     ToObservableCollection();

                                                                         }
                                                                     },_filmId);
        }

        public ObservableCollection<Comment> Comments
        {
            get { return (ObservableCollection<Comment>) grid.ItemsSource; }
            set { grid.ItemsSource = value; }
        }

        public override object CancelContent
        {
            get { return "Закрыть"; }
        }

        #endregion

        private void deleteComment_Clicked(object sender, RoutedEventArgs e)
        {
            var element = (FrameworkElement)sender;

            var comment = (Comment)element.DataContext;

            DoWork("Удаление комментария",(o, args) => Communication.Proxy.DeleteComment(args.Int),(sender1, eventArgs) =>
                                                                                                       {
                                                                                                           if(eventArgs.Error==null)
                                                                                                           {
                                                                                                               LoadInitialData();
                                                                                                           }
                                                                                                       },comment.id);
        }
        
        private void addComment_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new jAskForStringBox("Текст комментария:",string.Empty,true,new jNotEmptyStringRule(),null);
            dlg.ShowDialog(delegate(object o, object data)
                               {
                                   var str = (string)data;
                                   DoWork("Добавление комментария",(sender1, args) =>
                                                                       {
                                                                           args.Result =
                                                                               Communication.Proxy.AddComment(args.Int,
                                                                                                              (string)
                                                                                                              args.
                                                                                                                  Params
                                                                                                                  [1]);
                                                                       },(o1, eventArgs) =>
                                                                                              {
                                                                                                  if(eventArgs.Error==null)
                                                                                                  {
                                                                                                      Comments.Insert(0,(Comment) eventArgs.Result);
                                                                                                  }
                                                                                              },_filmId,str);
                               });
        }
    }
}
